var Session = (function() {
	var words, errorList;
	var index,count,offset,isReview;
	var level, offsetString, secLen, listLen, listLength,reviewMax, cIndex;

	function getCurrentWord() {
		//update window.currentWord
		var word = words[index].match(/^(\w*)\s(.*)$/);
		return {'def': word[2], 'en': word[1]};
	};

	function buildWordList() {
		// update words, the word Database, temporary
		function getText(level) {
			var fileName;
			if (level == "cet4") {
				fileName = "cet4.txt";
			};
			var xhr = new XMLHttpRequest();
			xhr.open("GET",fileName,false);
			xhr.send(null);
			return xhr.responseText;
		}
		var text = getText(level);
		var words = text.split(/\n/g);
		delete words[words.length-1]; // remove blank
		return words;
	};
	function buildOffset() {
		var offsetL = [];
		for (var i=0; i<offsetString.length; i++) {
			var list = parseInt(offsetString[i]);
			for (var j=0; j<secLen; j++) {
				offsetL.push((list-1)*secLen + j);
			}
		}
		return offsetL;
	};
	function getOffsetString() {
		var osString = readCookie('secCycle');
		if (!osString) {
			var tmp = [1];
			for (var i=2; i<=listLen; i++) {
				tmp.push(i);
				tmp.push(i-1);
				tmp.push(i);
			}
			osString = tmp.join('');
		}
		return osString;
	};
	function init() {
		checked = false;
		level = readCookie('level') || 'cet4';
		words = buildWordList(level);
		errorList = [];
		cIndex = index = parseInt(readCookie('index')) || 0;
		count = 0;
		currentWord = getCurrentWord();
		isReview = false;
		secLen = parseInt(readCookie('secLen')) || 5;
		listLen = parseInt(readCookie('listLen')) || 4;
		listLength = secLen * listLen;
		offsetString = getOffsetString();
		offset = buildOffset();
		reviewMax = parseInt(readCookie('reviewMax')) || 5;
	};
	function updateIndex() {
		count++;
		if (isReview) {
			var reviewLen;
			if (errorList.length > reviewMax)
				reviewLen = reviewMax;
			else reviewLen = errorList.length;
			if (count >= reviewLen ) {
				// change state
				count = 0;
				isReview = false;
				// update Index to next List
				oldIndex = parseInt(readCookie('index')) || 0;
				cIndex = index = oldIndex+listLength;
				createCookie('index',index,30);
			} else {
				index = errorList[count].index;
			}
		} else {
			if (count >= offset.length) {
				// change state
				count = 0;
				isReview = true;
				// update Index to Reiew List
				if (errorList.length > 0 ) {
					index = errorList[0].index;
				} else {
					isReview = false;
					oldIndex = parseInt(readCookie('index')) || 0;
					cIndex = index = oldIndex+listLength;
					createCookie('index',index,30);
				}
			} else {
				index = cIndex + offset[count];
			}
		}
	};
	return {
		getIndex : function() {
			return index;
		},
		updateCurrentWord : function() {
			updateIndex();
			currentWord = getCurrentWord();
		},
		addToErrorList : function(i) {
			var exist = false;
			for (item in errorList) {
				if (errorList[item].index == i) {
					exist = true;
					errorList[item].times++ ;
					break;
				}
			}
			if (!exist) {
				errorList.push({'index':i,'times':1});
			}
		},
		reset : init
	}
})();
